<?php

namespace MUD;

class Room{
    public $id;
    public $title;
    public $description;
    public $items;
    public $players;
    public $open=true;
    public $exit_north;
    public $exit_south;
    public $exit_east;
    public $exit_west;
    public $exit_up;
    public $exit_down;
    protected $code='';
    protected $key='';
    
    function __construct($id, $title, $description, $exits, $otherAttribs=array()){
        $this->id=$id;
        $this->title=$title;
        $this->description=$description;
        $this->exit_north=$exits['north'];
        $this->exit_south=$exits['south'];
        $this->exit_east=$exits['east'];
        $this->exit_west=$exits['west'];
        $this->exit_up=$exits['up'];
        $this->exit_down=$exits['down'];
        if(!empty($otherAttribs)){
            foreach($otherAttribs as $key => $value){
                $this->{$key}=$value;
            }
        }
    }
    
    public function showExits(){
        $back = R::current()->map->getLastRoom();
        $output = "Exits:\n";
        $output .= "N:\t" . $this->exit_north['title'] . "\n";
        $output .= "S:\t" . $this->exit_south['title'] . "\n";
        $output .= "E:\t" . $this->exit_east['title'] . "\n";
        $output .= "W:\t" . $this->exit_west['title'] . "\n";
        $output .= "U:\t" . $this->exit_up['title'] . "\n";
        $output .= "D:\t" . $this->exit_down['title'] . "\n";
        $output .= "B\t" . false === $back ? '' : $back->title;
        
        R::current()->client->send($output, "\n", true);
    }
    
    public function removeItem($item_id){
        if(empty($item_id)){
            return true;
        }
        for($i=0,$c=count($this->items);$i<$c;$i++){
            if($this->items[$i]->id==$item_id){
                $this->items[$i]=NULL;
                unset($this->items[$i]);
                $this->items=array_values($this->items);
                return true;
            }
        }
        return false;
    }
    
    public function addItem($item){
        if(!is_object($item)){
            return false;
        }
        $this->items[]=$item;
        return true;
    }
    
    /*
     * onLoad
     * @Description:    Fired when the room is loaded into memory. Loads any items and players in the room
     * @Return:         void
     */
    public function onLoad(){
        //load items
        $this->items=array();
        /*$loadItems=$this->R->dbo->select("item_id", "itemtracker", "location_id='".$this->id."' AND location_type='map'");
        if(!$loadItems){
            return false;
        }else{
            if($this->R->dbo->rows >= 1){
                for($i=0;$i<$this->R->dbo->rows;$i++){
                    $this->items[] = $this->R->items->getItem($this->R->dbo->result[$i]['item_id']);
                }
            }
        }*/
        //load players
        $this->players=array();
    }
    
    public function addItemToRoom($item_id){
        $this->items[] = $item_id;
    }
}